#include<iostream>
#include<fstream>
#include "Grammar.h"
#include "Config.h"
#include "Inference.h"
#include<sstream>
using namespace std;

inline string int2str(int x){
  stringstream ss;
  ss<<x;
  return ss.str();
}
int main(int argc,char **argv){
  Grammar a;

  string line;

  
  Config::load("hdppcfg.conf");
  a.initialize(20,16);
  a.writetofile("test.g");
  
  Inference inference(a);
  
  cout<<Config::getdouble("scale")<<" scale"<<endl;
 
  int maxiteratetime=Config::getint("maxiteratetime");
  for(int i=0;i<maxiteratetime;i++){
    ifstream fin;
    fin.open("data");
    cout<<"iteration time "<<i<<endl;
    assert(fin.is_open());
    inference.beforeinference(); 
    while(getline(fin,line)){
     // cout<<line<<endl;
      istringstream cins(line);
      vector<int> sentence;
      int tempid;
      while(cins>>tempid){
	sentence.push_back(tempid);
      }
      inference.insideoutside(sentence);
    }
    inference.updategrammar();
    fin.close();
    
    if(i%10==0){
      string si=int2str(i);
      a.writetofile("result"+si+".g");
    }
    if(inference.isconverge()){
      cout<<"converge at "<<i<<" time"<<endl;
      break;
    }
  }
  a.writetofile("result.g");
  
  return 1;
}
 

